package com.handpay.zztong.hp.setting.activity;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.ZZTApplication;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.setting.model.VersionInfoBean;

import java.util.Hashtable;

public class AboutActivity extends ZZTongActivity {

    private TextView clientName;
    private TextView versionNumber;
    private LinearLayout ll_version_update;
    private LinearLayout ll_currentVersion_instruction;
    private TextView versionTextView;
    private ImageView arrow;
    private String description;
    private String updateUrl;
    private String version, forcedUpdate;


    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.Version_center), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_about);
        super.onCreate(savedInstanceState);
        clientName = (TextView) findViewById(R.id.app_client_name);
        clientName.setText(getResources().getString(R.string.app_name) + getResources().getString(R.string.vpos_client));
        versionNumber = (TextView) findViewById(R.id.current_version_number);
        versionTextView = (TextView) findViewById(R.id.tv_version_update_tip);
        ll_version_update = (LinearLayout) findViewById(R.id.ll_version_update);
        ll_currentVersion_instruction = (LinearLayout) findViewById(R.id.ll_Version_instruction);
        arrow = (ImageView) findViewById(R.id.arrow_tip);
        ll_currentVersion_instruction.setOnClickListener(this);
        versionNumber.setText(getResources().getString(R.string.current_version_number) + ClientEngine.VERSION);// ZZTConfig.testerVersion
        doVerifyVersion(AboutActivity.this);
        initdata();
    }

    /***
     * 初始化版本检测相关数据
     */
    private void initdata() {
        version = (String) ClientEngine.getInstance().getGlobal(VERSION);
        description = (String) ClientEngine.getInstance().getGlobal(DESCRIPTION);
        updateUrl = (String) ClientEngine.getInstance().getGlobal(UPDATEURL);
        forcedUpdate = (String) ClientEngine.getInstance().getGlobal(FORCEDUPDATE);
        int result = version.compareTo(ClientEngine.VERSION);
        HPLog.d(HPLog.SSX_TAG, "版本比较结果:" + result);
        if (result < 0 || result == 0) {
            versionTextView.setText(getResources().getString(R.string.current_version_new));
            ll_version_update.setOnClickListener(null);
        } else {
            versionTextView.setText(getResources().getString(R.string.find_version_new));
            arrow.setVisibility(View.VISIBLE);
            arrow.setBackgroundResource(R.drawable.smallreddot);
            ll_version_update.setOnClickListener(this);
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp, boolean showAlert, OnClickListener on) {
        super.netResponse(action, resp, showAlert, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ZZTApplication.getApp().exitApp();
                System.exit(0);
            }
        });
        version = (String) ClientEngine.getInstance().getGlobal(VERSION);
        description = (String) ClientEngine.getInstance().getGlobal(DESCRIPTION);
        updateUrl = (String) ClientEngine.getInstance().getGlobal(UPDATEURL);
        forcedUpdate = (String) ClientEngine.getInstance().getGlobal(FORCEDUPDATE);
        description = description.replace("&", "\n");
        HPLog.i("AboutActivity", "version:" + version + "url:" + updateUrl + "description:" + description);

        int result = version.compareTo(ClientEngine.VERSION);
        HPLog.i("AboutActivity----------------------", "result:" + result);
        if (result < 0 || result == 0) {
            versionTextView.setText(getResources().getString(R.string.current_version_new));
            ll_version_update.setOnClickListener(null);
        } else {
            versionTextView.setText(getResources().getString(R.string.find_version_new));
            arrow.setVisibility(View.VISIBLE);
            arrow.setBackgroundResource(R.drawable.smallreddot);
            ll_version_update.setOnClickListener(this);
        }
        return false;
    }

    /**
     * 版本检测处理流程
     */
    protected void isUpdateVersion(final ZZTongActivity activity, String title, String message, final String url) {
        showAlertDialog(activity, title, message, false, getResources().getString(R.string.immediately_update), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    // ClientEngine.getInstance().browse(activity, url,
                    // false);
                    goToDownloadActivity(activity, url);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }, getResources().getString(R.string.not_update), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, null, null);
    }

    public void doVersionDesc() {
        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put("version", ClientEngine.VERSION);
        params.put("platform", ClientEngine.MOBILETYPE);
        params.put("channel", ZZTConfig.UPDATE_CHANNEL);
        doJsonPost(this, Do_ZztVersionDesc, VersionInfoBean.class, params);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.ll_version_update) {
            String message = description;
            String title = getString(R.string.updatetitle, version);
            if ("0".equals(forcedUpdate)) {
                isUpdateVersion(AboutActivity.this, title, message, updateUrl);
            } else {
                checkVersion(AboutActivity.this, title, message, updateUrl);
            }
        } else if (i == R.id.ll_Version_instruction) {
            doVersionDesc();
        }
    }

    @Override
    public boolean netJsonResponse(String action, ResponseBean resp, boolean showAlert, OnClickListener on) {
        if (super.netJsonResponse(action, resp, showAlert, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })) {
            return true;
        }
        if (action.equals(Do_ZztVersionDesc)) {
            VersionInfoBean versionInfoBean = (VersionInfoBean) resp;
            if (versionInfoBean.getResponseCode().equals("0")) {
                Intent intent = new Intent(this, VersionIntroductionActivity.class);
                intent.putExtra("versionInfoBean", versionInfoBean);
                this.startActivity(intent);
                return false;
            } else {
                showAlertDialog(AboutActivity.this, getString(R.string.prompt), versionInfoBean.getErrMessage());
                return true;
            }
        }
        return true;
    }
}
